module Lypp
  class WriteOnlyError < RuntimeError #:nodoc:
  end
  class ReadOnlyError < RuntimeError #:nodoc:
  end

  class Base < ActiveResource::Base #:nodoc:
    class << self
      def path_prefix
        ''
      end

      def site
        (@site || superclass.site.dup) + path_prefix
      end

      [ :username, :password, :user_id ].each do |attribute|
        define_method(attribute) { config[attribute] }
      end

      private
        def config
          @config ||= YAML.load_file(Pathname.new(RAILS_ROOT) + 'config/lypp.yml').symbolize_keys
        end
    end

    self.site = returning URI.parse(config[:site] || 'https://lypp.com') do |site|
      site.user, site.password = self.username, self.password
    end
  end
end
